﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Greve
{
    public partial class NewJumper : Form
    {
		private bool keyPress = false;
		private bool err = false;
		private bool useExistingName = false;
		public bool cancel = true;
        public Person p {get; set;}
        int _max;
		int _height;
		Dictionary<int, String> _list;
		List<JumpCode> _codes;
        public NewJumper(int maxJumps,String gender, int jumpHeight, Dictionary<int,String> list, List<JumpCode> codes)
        {
			InitializeComponent();
			
			
			_codes = codes;
			_list = list;
            _max = maxJumps;
			_height = jumpHeight;

			p = new Person();
            p.gender = textBoxGender.Text = gender;
			if(_list != null)
				foreach (KeyValuePair<int, String> v in _list)
					listBoxNames.Items.Add((v.Key+" "+v.Value).ToString());



			this.KeyPreview = true;
			this.KeyDown += new KeyEventHandler(NewJumper_KeyDown);
        }

        private void IDBox(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
		
		private void setText()
		{
			p.name = textBoxName.Text;
			useExistingName = false;
		}
		
		private void textBoxName_TextChanged(object sender, EventArgs e)
		{
			setText();
		}

		void FAddDiver()
		{
			if (textBoxName.Text.Equals("") || p.ID == 0)
				MessageBox.Show("Missing name or ID");
			else
			{
				if (!useExistingName)
				{
					foreach (KeyValuePair<int, String> v in _list)
					{
						if (v.Key.Equals(p.ID))
						{
							List<int> list = new List<int>();
							foreach (int i in _list.Keys)
								list.Add(i);
							MessageBox.Show("ID already taken.", "Duplicate IDs", MessageBoxButtons.OK);
							new UsedIDs(list).ShowDialog();
							err = true;
						}
					}
					if (!err)
					{
						_list.Add(p.ID, p.name);
						cancel = false;
						this.Close();
						err = false;
					}
					else
					{
						err = false;
					}
				}
				else
				{
					cancel = false;
					this.Close();
				}
			}
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			FAddDiver();
		}

		void FAddJump()
		{
			
			if (p.jumps.Count == _max)
				MessageBox.Show("Max number of jumps added", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
			else
			{
				AddJump addjump = new AddJump(_codes, _height);
                addjump.ShowDialog();
                if (addjump.b && p.jumps.Count != _max)
                {

                    int i = 0;
                    foreach (Dive d in addjump.dd)
                    {
                        if (i == _max)
                        {
                            MessageBox.Show("Max number of jumps added, Will skip the rest", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                        }
                        else
                        {
                            p.jumps.Add(d);
                            listBoxDives.Items.Add(d.ID + " " + d.jump);
                            ++i;
                        }
                    }
                }
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			cancel = true;
			this.Close();
		}

		private void textBoxID_TextChanged(object sender, EventArgs e)
		{
			if (!textBoxID.Text.Equals(""))
			{
				p.ID = int.Parse(textBoxID.Text);
				useExistingName = false;
			}
		}

		private void listBoxNames_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (listBoxNames.SelectedItem != null)
			{
				p.name = textBoxName.Text = listBoxNames.SelectedItem.ToString().Split(' ')[1];
				p.ID = int.Parse(textBoxID.Text = listBoxNames.SelectedItem.ToString().Split(' ')[0]);
				useExistingName = true;
			}
		}

		private void buttonAddJump_Click(object sender, EventArgs e)
		{
			FAddJump();
		}

		void FRemoveJump()
		{
			if (listBoxDives.SelectedIndex != -1)
			{
				p.jumps.RemoveAt(listBoxDives.SelectedIndex);
				listBoxDives.Items.Remove(listBoxDives.SelectedItem);
			}
		}
		private void buttonRemoveJump_Click(object sender, EventArgs e)
		{
			FRemoveJump();
		}

		private void NewJumper_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode.Equals(Keys.A) && !keyPress)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				keyPress = true;
				listBoxDives.Focus();
				FAddJump();
			}
			else if (e.Control && e.KeyCode.Equals(Keys.D) && !keyPress)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				keyPress = true;
				FRemoveJump();
			}
			else if (e.Control && e.KeyCode.Equals(Keys.N) && !keyPress)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				keyPress = true;
				textBoxName.Focus();
				textBoxName.SelectAll();
			}
			else if (e.Control && e.KeyCode.Equals(Keys.I) && !keyPress)
			{
				e.Handled = true;
				e.SuppressKeyPress = true;
				keyPress = true;
				textBoxID.Focus();
				textBoxID.SelectAll();
			}
		}

		private void NewJumper_KeyUp(object sender, KeyEventArgs e)
		{
			e.Handled = true;
			keyPress = false;
		}
    }
}
